package com.mindata.blockchain.core.sqlite.config;

import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author wuweifeng wrote on 2018/3/2.
 */
@Configuration
@EnableJpaRepositories(basePackages = "com.mindata.blockchain.core.repository", transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "localContainerEntityManagerFactoryBean")
@EnableTransactionManagement
public class JpaConfiguration {
	@Resource
	private JpaProperties jpaProperties;

	@Autowired
	@Bean
	public JpaTransactionManager jpaTransactionManager(
			@Qualifier(value = "EmbeddeddataSource") final DataSource dataSource,
			final EntityManagerFactory entityManagerFactory) {
		final JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
		jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
		jpaTransactionManager.setDataSource(dataSource);

		return jpaTransactionManager;
	}

	@Autowired
	@Bean
	LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(
			@Qualifier(value = "EmbeddeddataSource") final DataSource dataSource,
			final EntityManagerFactoryBuilder builder) {
		return builder.dataSource(dataSource).packages("com.mindata.blockchain.core.model")
				.properties(getVendorProperties(dataSource)).build();
	}

	private Map<String, String> getVendorProperties(final DataSource dataSource) {
		return jpaProperties.getHibernateProperties(dataSource);
	}

}